# -*- coding:UTF-8 -*-

from pyecharts.charts import Line
from pyecharts import options as opt
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from src.bridge.getsqldata import MysqlData


class AnaEcharts:
    def __init__(self):
        self.md = MysqlData()
        self._pic_line()

    def _pic_line(self):
        self.data = self.md.sqlite_dayport_data('1=1')
        if self.data:
            day = set()
            done, closed, cancel = [], [], []
            for d in range(len(self.data)):
                day.add(self.data[d][0])
            dayl = list(day)
            dayl.sort(reverse=False)
            for d in dayl:
                doneres = self.md.count_data('dayport_data', condition="daytime ='{}' AND newest_v ='done'".format(d))
                done.append(doneres[0][0])
                closedres = self.md.count_data('dayport_data',
                                               condition="daytime ='{}' AND newest_v ='closed'".format(d))
                closed.append(closedres[0][0])
                cancelres = self.md.count_data('dayport_data',
                                               condition="daytime ='{}' AND newest_v ='cancel'".format(d))
                cancel.append(cancelres[0][0])
            line = Line(init_opts=opt.InitOpts(theme=ThemeType.CHALK, width="700px", height="300px"))
            line.add_xaxis(dayl)
            line.add_yaxis('完成', done, is_smooth=True)
            line.add_yaxis('关闭', closed, is_smooth=True)
            line.add_yaxis('取消', cancel, is_smooth=True)
            line.set_global_opts(legend_opts=opt.LegendOpts(pos_top='5%', pos_right='3%'))
            make_snapshot(snapshot, line.render(), './confile/analysisdayport.png')
